package com.software.dao;


import com.software.model.User;
import com.software.utils.DatabaseConnection;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class UserDAO {
    public List<User> getAllUsers() throws SQLException {
        List<User> userList = new ArrayList<>();
        String query = "SELECT * FROM users";

        try (Connection conn = DatabaseConnection.getConnection();
             PreparedStatement stmt = conn.prepareStatement(query);
             ResultSet rs = stmt.executeQuery()) {

            while (rs.next()) {
                User user = new User(rs.getInt("id"), rs.getString("nickname"), rs.getString("username"),
                        rs.getString("password"), rs.getString("email"),
                        rs.getString("phone"), rs.getInt("status"));
                userList.add(user);
            }
        }
        return userList;
    }

    public User getUserById(int userId) throws SQLException {
        String query = "SELECT * FROM users WHERE id = ?";
        User user = null;

        try (Connection conn = DatabaseConnection.getConnection();
             PreparedStatement stmt = conn.prepareStatement(query)) {
            stmt.setInt(1, userId);
            ResultSet rs = stmt.executeQuery();
            if (rs.next()) {
                user = new User(rs.getInt("id"), rs.getString("nickname"), rs.getString("username"),
                        rs.getString("password"), rs.getString("email"),
                        rs.getString("phone"), rs.getInt("status"));
            }
        }
        return user;
    }
}
